package com.yeyks.service.impl;

import com.yeyks.base.PageInfo;
import com.yeyks.dal.dao.FishBallGoodsMapper;
import com.yeyks.dal.domain.FishBall;
import com.yeyks.dal.domain.FishBallBill;
import com.yeyks.dal.domain.FishBallGoods;
import com.yeyks.dal.domain.UserInfo;
import com.yeyks.dto.FishBallGoodsDTO;
import com.yeyks.exception.ServiceException;
import com.yeyks.model.enums.FishBallEnum;
import com.yeyks.service.*;
import com.yeyks.util.ObjectConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/24 0024
 */
@Service
@Slf4j
public class FishBallGoodsServiceImpl implements FishBallGoodsService {

    @Autowired
    private FishBallService fishBallService;
    @Autowired
    private UserCouponService userCouponService;
    @Autowired
    private FishBallGoodsMapper goodsMapper;

    /**
     * 消费商品券
     *
     * @param userInfo
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Integer conversion(UserInfo userInfo, Integer id) {
        try {
            FishBall fishBall = fishBallService.getFishBallNoCreate(userInfo.getId());
            if (fishBall == null) {
                log.info("该用户鱼丸账号为空userId = {}", userInfo.getId());
                throw new ServiceException("该用户鱼丸账号为空");
            }
            //根据券id获取商品券
            FishBallGoods fishBallGoods = goodsMapper.selectByPrimaryKey(id);
            if (fishBallGoods == null) {
                log.info("没有该券对应的券商品id = {}", id);
                throw new ServiceException("没有该券对应的券商品");
            }
            Integer couponId = fishBallGoods.getCouponId();
            int totelAmount = fishBall.getTotalAmount() == null ? 0 : fishBall.getTotalAmount();
            int useAmount = fishBall.getUseAmount() == null ? 0 : fishBall.getUseAmount();
            int remainAmount = totelAmount - useAmount;
            int count = fishBallGoods.getNeedFishballCount() == null ? 0 : fishBallGoods.getNeedFishballCount();
            if (remainAmount < count) {
                log.info("账户鱼丸不足remain = {}, count = {}", remainAmount, count);
                throw new ServiceException("账户鱼丸不足");
            }
            int goodsCount = fishBallGoods.getCount() == null ? 0 : fishBallGoods.getCount();
            if (goodsCount == 0) {
                log.info("该券已兑完id = {}", id);
                throw new ServiceException("该券已兑完");
            }
            Boolean flag = userCouponService.userGetFishBallCoupon(userInfo.getId(), couponId);
            if (!flag) {
                log.info("用户领取券异常{}{}", userInfo.getId(), couponId);
                throw new ServiceException("用户领取券异常");
            }
            //减少商品券总数
            int newGoodsCount = goodsCount - 1 < 0 ? 0 : goodsCount - 1;
            fishBallGoods.setCount(newGoodsCount);
            int res = goodsMapper.updateByPrimaryKey(fishBallGoods);
            if (res != 1) {
                log.info("更新商品券的个数异常");
                throw new ServiceException("更新商品券异常");
            }
            return fishBallService.descFishBall(count, userInfo.getId(), FishBallEnum.GOODS.desc(), 2);
        } catch (ServiceException e) {
            throw new ServiceException(e.getMessage());
        } catch (Exception e) {
            log.error("消费商品券异常{}", e.getLocalizedMessage());
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 分页获取商品券
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<FishBallGoodsDTO> list(Integer pageNo, Integer pageSize) {
        PageInfo of = PageInfo.of(pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("limit", of.getLimit());
        map.put("offset", of.getOffset());
        List<FishBallGoods> fishBallGoods = goodsMapper.list(map);
        return getFishBallGoodsDTO(fishBallGoods);
    }

    private List<FishBallGoodsDTO> getFishBallGoodsDTO(List<FishBallGoods> fishBallGoods) {
        List<FishBallGoodsDTO> fishBallGoodsDTOS = new ArrayList<>();
        for (FishBallGoods fishBallGood : fishBallGoods) {
            FishBallGoodsDTO fishBallGoodsDTO = ObjectConvertUtil.convertObject(fishBallGood, FishBallGoodsDTO.class);
            fishBallGoodsDTOS.add(fishBallGoodsDTO);
        }
        return fishBallGoodsDTOS;
    }
}
